package com.datamation.swadeshi.control;

import java.util.ArrayList;

import com.datamation.swadeshi.control.data.FreeDebDS;
import com.datamation.swadeshi.control.data.FreeDetDS;
import com.datamation.swadeshi.control.data.FreeHedDS;
import com.datamation.swadeshi.control.data.FreeItemDS;
import com.datamation.swadeshi.control.data.FreeMslabDS;
import com.datamation.swadeshi.control.data.FreeSlabDS;
import com.datamation.swadeshi.control.data.OrdHedDS;
import com.datamation.swadeshi.model.FreeHed;
import com.datamation.swadeshi.model.FreeItem;
import com.datamation.swadeshi.model.FreeItemDetails;
import com.datamation.swadeshi.model.FreeMslab;
import com.datamation.swadeshi.model.FreeSlab;
import com.datamation.swadeshi.model.InvDet;
import com.datamation.swadeshi.model.OrdDet;

import android.content.Context;
import android.util.Log;

public class FreeIssue {
	Context context;

	public FreeIssue(Context context) {
		this.context = context;
	}
	
	
	public ArrayList<FreeItemDetails> getFreeItemsBySalesItem(ArrayList<OrdDet> dets){
		//final return array list
		ArrayList<FreeItemDetails> freeList= new ArrayList<FreeItemDetails>();
		
		FreeHedDS freeHedDS =new FreeHedDS(context);
		int slabassorted=0, flatAssort=0, mixAssort=0;
		for (OrdDet det : dets) {//---------------------- order list --------------------

			Log.v("forddet", det.getFORDDET_ITEM_CODE());
			
			ArrayList<FreeHed> arrayList=freeHedDS.getFreeIssueItemDetailByRefno(det.getFORDDET_ITEM_CODE());
			
			int entedTotQty=Integer.parseInt(det.getFORDDET_QTY());	
			
			if (entedTotQty>0){
				
				for (FreeHed freeHed : arrayList) {//--------Related free issue list------
					
					if(freeHed.getFFREEHED_PRIORITY().equals("1")){ 
						//flat
						if(freeHed.getFFREEHED_FTYPE().equals("Flat")){//-------Flat only-----
													
							int itemQty= (int) Float.parseFloat(freeHed.getFFREEHED_ITEM_QTY());
							String debCode= new OrdHedDS(context).getRefnoByDebcode(det.getFORDDET_REFNO());
							int debCount=new FreeDebDS(context).getRefnoByDebCount(freeHed.getFFREEHED_REFNO());
							int IsValidDeb=new FreeDebDS(context).isValidDebForFreeIssue(freeHed.getFFREEHED_REFNO(),debCode);
							
							int assortCount=new FreeDetDS(context).getAssoCountByRefno(freeHed.getFFREEHED_REFNO());
							if(assortCount>1){
								
								flatAssort= flatAssort + entedTotQty;
								
								int index=0;
								boolean assortUpdate=false;
								
								for (FreeItemDetails freeItemDetails : freeList) {
										if(freeItemDetails.getRefno().equals(freeHed.getFFREEHED_REFNO())){
											
											freeList.get(index).setRefno(freeHed.getFFREEHED_REFNO());
											freeList.get(index).setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
											freeList.get(index).setFreeQty((int) Math.round(flatAssort / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY()));
											assortUpdate=true;
										}
										
										index++;
									}
								
								if(!assortUpdate){//When 1st time running
									
									if (debCount>0 ){
										
										if(IsValidDeb==1){
											
											if((int) Math.round(entedTotQty/itemQty)>0){
												
												FreeItemDetails details = new FreeItemDetails();
												//details.setFreeItem(new FreeItemDS(context).getFreeItemsByRefno(freeHed.getFFREEHED_REFNO()));
												details.setRefno(freeHed.getFFREEHED_REFNO());
												details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
												details.setFreeQty((int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY()));
												freeList.add(details);
												
												Log.v("Free Issues ", (int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY())+"");
												entedTotQty=(int) Math.round(entedTotQty % itemQty);
											}
										}
								
									}else{
										
										if((int) Math.round(entedTotQty/itemQty)>0){
											
											FreeItemDetails details = new FreeItemDetails();
											//details.setFreeItem(new FreeItemDS(context).getFreeItemsByRefno(freeHed.getFFREEHED_REFNO()));
											details.setRefno(freeHed.getFFREEHED_REFNO());
											details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
											details.setFreeQty((int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY()));
											freeList.add(details);
											
											Log.v("Free Issues ", (int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY())+"");
											entedTotQty=(int) Math.round(entedTotQty % itemQty);
										}
									}
								}
								

															
							}else{
								
								if (debCount>0 ){
									
										if(IsValidDeb==1){
											
											if((int) Math.round(entedTotQty/itemQty)>0){
												
												FreeItemDetails details = new FreeItemDetails();
												//details.setFreeItem(new FreeItemDS(context).getFreeItemsByRefno(freeHed.getFFREEHED_REFNO()));
												details.setRefno(freeHed.getFFREEHED_REFNO());
												details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
												details.setFreeQty((int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY()));
												freeList.add(details);
												
												Log.v("Free Issues ", (int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY())+"");
												entedTotQty=(int) Math.round(entedTotQty % itemQty);
											}
										}
								
									}else{
										
										if((int) Math.round(entedTotQty/itemQty)>0){
											
											FreeItemDetails details = new FreeItemDetails();
											//details.setFreeItem(new FreeItemDS(context).getFreeItemsByRefno(freeHed.getFFREEHED_REFNO()));
											details.setRefno(freeHed.getFFREEHED_REFNO());
											details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
											details.setFreeQty((int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY()));
											freeList.add(details);
											
											Log.v("Free Issues ", (int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY())+"");
											entedTotQty=(int) Math.round(entedTotQty % itemQty);
										}
									}
								}
						}else if(freeHed.getFFREEHED_FTYPE().equals("Slab")){//-------Slab only-----
							
								FreeSlabDS freeSlabDS=new FreeSlabDS(context);				
								final ArrayList<FreeSlab> slabList;
								int assortCount=new FreeDetDS(context).getAssoCountByRefno(freeHed.getFFREEHED_REFNO());
								if(assortCount>1){
									
									slabassorted= slabassorted + entedTotQty;
									slabList=freeSlabDS.getSlabdetails(freeHed.getFFREEHED_REFNO(), slabassorted);
								
								}else{
								
									slabList=freeSlabDS.getSlabdetails(freeHed.getFFREEHED_REFNO(), entedTotQty);
									
								}
								
															
								for (FreeSlab freeSlab : slabList) {
								
									String debCode= new OrdHedDS(context).getRefnoByDebcode(det.getFORDDET_REFNO());
									int debCount=new FreeDebDS(context).getRefnoByDebCount(freeHed.getFFREEHED_REFNO());
									int IsValidDeb=new FreeDebDS(context).isValidDebForFreeIssue(freeHed.getFFREEHED_REFNO(),debCode);
																		
									if (debCount>0){//selected debtors
									
										if(IsValidDeb==1){
										
											if(assortCount>1){
												
												int index=0;
												boolean assortUpdate=false;
												
												for (FreeItemDetails freeItemDetails : freeList) {
														if(freeItemDetails.getRefno().equals(freeHed.getFFREEHED_REFNO())){
															//details=freeItemDetails;
															freeList.get(index).setFreeQty((int) (Float.parseFloat(freeSlab.getFFREESLAB_FREE_QTY())));
															freeList.get(index).setRefno(freeHed.getFFREEHED_REFNO());
															freeList.get(index).setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
															assortUpdate=true;
														}
														
														index++;
													}
											
												if(!assortUpdate){
													
													Log.v("Stab", freeSlab.getFFREESLAB_FREE_QTY());
													FreeItemDetails details = new FreeItemDetails();
													details.setRefno(freeHed.getFFREEHED_REFNO());
													details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
													details.setFreeQty((int) (Float.parseFloat(freeSlab.getFFREESLAB_FREE_QTY())));
													freeList.add(details);
												}
												
											}else{	
												Log.v("Stab", freeSlab.getFFREESLAB_FREE_QTY());
												FreeItemDetails details = new FreeItemDetails();
												details.setRefno(freeHed.getFFREEHED_REFNO());
												details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
												details.setFreeQty((int) (Float.parseFloat(freeSlab.getFFREESLAB_FREE_QTY())));
												freeList.add(details);
											}
										}
									
									}else{//all debtor for freeissues
										
										if(assortCount>1){
											int index=0;
											boolean assortUpdate=false;
											for (FreeItemDetails freeItemDetails : freeList) {
													if(freeItemDetails.getRefno().equals(freeHed.getFFREEHED_REFNO())){
														//details=freeItemDetails;
														freeList.get(index).setFreeQty((int) (Float.parseFloat(freeSlab.getFFREESLAB_FREE_QTY())));
														freeList.get(index).setRefno(freeHed.getFFREEHED_REFNO());
														freeList.get(index).setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
														assortUpdate=true;
													}
													
													index++;
												}
											
											if(!assortUpdate){
												
												Log.v("Stab", freeSlab.getFFREESLAB_FREE_QTY());
												FreeItemDetails details = new FreeItemDetails();
												details.setRefno(freeHed.getFFREEHED_REFNO());
												details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
												details.setFreeQty((int) (Float.parseFloat(freeSlab.getFFREESLAB_FREE_QTY())));
												freeList.add(details);
											}
											
										}else{
											Log.v("Stab", freeSlab.getFFREESLAB_FREE_QTY());
											FreeItemDetails details = new FreeItemDetails();
											details.setRefno(freeHed.getFFREEHED_REFNO());
											details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
											details.setFreeQty((int) (Float.parseFloat(freeSlab.getFFREESLAB_FREE_QTY())));
											freeList.add(details);
										}
									}
							}

						}else if(freeHed.getFFREEHED_FTYPE().equals("Mix")){
							//slabAssort
							
							FreeMslabDS freeMslabDS =new FreeMslabDS(context);				
							final ArrayList<FreeMslab> mixList;
							int assortCount=new FreeDetDS(context).getAssoCountByRefno(freeHed.getFFREEHED_REFNO());
							if(assortCount>1){
								mixAssort= mixAssort + entedTotQty;
								mixList=freeMslabDS.getMixDetails(freeHed.getFFREEHED_REFNO(), mixAssort);
							}else{
								mixList=freeMslabDS.getMixDetails(freeHed.getFFREEHED_REFNO(), entedTotQty);
							}
							
							for (FreeMslab freeMslab : mixList) {
								
								String debCode= new OrdHedDS(context).getRefnoByDebcode(det.getFORDDET_REFNO());
								int debCount=new FreeDebDS(context).getRefnoByDebCount(freeHed.getFFREEHED_REFNO());
								int IsValidDeb=new FreeDebDS(context).isValidDebForFreeIssue(freeHed.getFFREEHED_REFNO(),debCode);
																	
								if (debCount>0){//selected debtors
								
									if(IsValidDeb==1){
									
										if(assortCount>1){
											
											int index=0;
											boolean assortUpdate=false;
											
											for (FreeItemDetails freeItemDetails : freeList) {
													if(freeItemDetails.getRefno().equals(freeHed.getFFREEHED_REFNO())){
																									
														int itemQty= (int) Float.parseFloat(freeMslab.getFFREEMSLAB_ITEM_QTY());
														freeList.get(index).setRefno(freeHed.getFFREEHED_REFNO());
														freeList.get(index).setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
														freeList.get(index).setFreeQty((int) Math.round(mixAssort / itemQty) * (int) Float.parseFloat(freeMslab.getFFREEMSLAB_FREE_IT_QTY()));
																												
														assortUpdate=true;
													}
													index++;
												}
										
											if(!assortUpdate){
												
												FreeItemDetails details = new FreeItemDetails();
												int itemQty= (int) Float.parseFloat(freeMslab.getFFREEMSLAB_ITEM_QTY());
												details.setRefno(freeHed.getFFREEHED_REFNO());
												details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
												details.setFreeQty((int) Math.round(mixAssort / itemQty) * (int) Float.parseFloat(freeMslab.getFFREEMSLAB_FREE_IT_QTY()));
												freeList.add(details);
											}
											
										}else{	

											FreeItemDetails details = new FreeItemDetails();
											int itemQty= (int) Float.parseFloat(freeMslab.getFFREEMSLAB_ITEM_QTY());
											details.setRefno(freeHed.getFFREEHED_REFNO());
											details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
											details.setFreeQty((int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeMslab.getFFREEMSLAB_FREE_IT_QTY()));
											freeList.add(details);
											
										}
									}
								
								}else{//all debtor for freeissues
									
									if(assortCount>1){
										int index=0;
										boolean assortUpdate=false;
										for (FreeItemDetails freeItemDetails : freeList) {
												if(freeItemDetails.getRefno().equals(freeHed.getFFREEHED_REFNO())){
													
													int itemQty= (int) Float.parseFloat(freeMslab.getFFREEMSLAB_ITEM_QTY());
													freeList.get(index).setRefno(freeHed.getFFREEHED_REFNO());
													freeList.get(index).setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
													freeList.get(index).setFreeQty((int) Math.round(mixAssort / itemQty) * (int) Float.parseFloat(freeMslab.getFFREEMSLAB_FREE_IT_QTY()));
																											
													assortUpdate=true;
												}
												
												index++;
											}
										
										if(!assortUpdate){
											
											FreeItemDetails details = new FreeItemDetails();
											int itemQty= (int) Float.parseFloat(freeMslab.getFFREEMSLAB_ITEM_QTY());
											details.setRefno(freeHed.getFFREEHED_REFNO());
											details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
											details.setFreeQty((int) Math.round(mixAssort / itemQty) * (int) Float.parseFloat(freeMslab.getFFREEMSLAB_FREE_IT_QTY()));
											freeList.add(details);
										}
										
									}else{
										FreeItemDetails details = new FreeItemDetails();
										int itemQty= (int) Float.parseFloat(freeMslab.getFFREEMSLAB_ITEM_QTY());
										details.setRefno(freeHed.getFFREEHED_REFNO());
										details.setFreeIssueSelectedItem(det.getFORDDET_ITEM_CODE());
										details.setFreeQty((int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeMslab.getFFREEMSLAB_FREE_IT_QTY()));
										freeList.add(details);
									}
								}
						}
						}
					}
				}
			}
		}
		
		return freeList;
		
	}
	
	/*
	 * ============================ Van Sales=========================
	 */
	
	public ArrayList<FreeItemDetails> getFreeItemsByVanSalesItem(ArrayList<InvDet> invDets){
		//final return array list
		ArrayList<FreeItemDetails> freeList= new ArrayList<FreeItemDetails>();
		
		FreeHedDS freeHedDS =new FreeHedDS(context);
		for (InvDet invDet : invDets) {//---------------------- order list --------------------

			Log.v("forddet", invDet.getFINVDET_ITEM_CODE());
			
			ArrayList<FreeHed> arrayList=freeHedDS.getFreeIssueItemDetailByRefno(invDet.getFINVDET_ITEM_CODE());
			
			int entedTotQty=Integer.parseInt(invDet.getFINVDET_QTY());	
			
			if (entedTotQty>0){
				
				for (FreeHed freeHed : arrayList) {//--------Related free issue list------
					
					if(freeHed.getFFREEHED_PRIORITY().equals("1")){ 
						//flat
						if(freeHed.getFFREEHED_FTYPE().equals("Flat")){//-------Flat only-----
							
								int itemQty= (int) Float.parseFloat(freeHed.getFFREEHED_ITEM_QTY());
									
								//Log.v("Total Enter Qty", entedTotQty+"");
								//Log.v("Free Per Qty", itemQty+"");
								//Log.v("Free Items Qty / ", (int) Math.round(entedTotQty / itemQty)+"");
								//Log.v("Free Items Qty % ", (int) Math.round(entedTotQty % itemQty)+"");
								
								
								
								if((int) Math.round(entedTotQty/itemQty)>0){
									
									FreeItemDetails details = new FreeItemDetails();
									//details.setFreeItem(new FreeItemDS(context).getFreeItemsByRefno(freeHed.getFFREEHED_REFNO()));
									details.setRefno(freeHed.getFFREEHED_REFNO());
									details.setFreeIssueSelectedItem(invDet.getFINVDET_ITEM_CODE());
									details.setFreeQty((int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY()));
									
									freeList.add(details);
									
									Log.v("Free Issues ", (int) Math.round(entedTotQty / itemQty) * (int) Float.parseFloat(freeHed.getFFREEHED_FREE_IT_QTY())+"");
									
									entedTotQty=(int) Math.round(entedTotQty % itemQty);
								}
							
						}
					}
				}
			}
		}
		
		return freeList;
		
	}	
}
